#include "map.h"

void Map_C::Init()
{
	m_bSucess=false;
	m_State = STATE_PLAY;
	m_SceneX = m_SceneY = 0;

	m_Scene[0].Init(0,"scene/scene0.xml");
	m_Scene[1].Init(1,"scene/scene1.xml");
	m_CurScene = m_Scene;
	m_OldScene = NULL;
}

int Map_C::Update(float dt,Role_C &role)
{
	switch( m_State )
	{
	case STATE_PLAY:
		if( m_CurScene->Update(dt,role) )
		{
			m_OldScene = m_CurScene;
			m_CurScene = GetNextScene(m_CurScene->m_ID);
			if( m_CurScene == m_OldScene )
				m_bSucess = true;
			else
				m_State = STATE_CHANGE;
		}
		break;
	case STATE_CHANGE:
		m_SceneX -= 300*dt;
		role.m_Rect.x -=  300*dt;
		if( m_SceneX <= -SCREEN_WIDTH )
		{
			m_State = STATE_PLAY;
			m_SceneX = 0;
		}
		break;
	default:
		break;
	}
	return m_State;
}

void Map_C::Draw()
{
	switch( m_State )
	{
	case STATE_PLAY:
		if( m_CurScene )
			m_CurScene->Draw(0,0);
		break;
	case STATE_CHANGE:
		m_OldScene->Draw(m_SceneX,m_SceneY);
		m_CurScene->Draw(m_SceneX+SCREEN_WIDTH,m_SceneY);
		break;
	default:
		break;
	}
}

void Map_C::Exit()
{
	m_Scene[0].Exit();
	m_Scene[1].Exit();
}

bool Map_C::Sucess()
{
	return m_bSucess;
}

Scene_C* Map_C::GetNextScene(int cur_id)
{
	if( cur_id == 0 )
		return m_Scene+1;
	else
		return m_Scene+cur_id;
}